package com.cn.zpark.utils;

import java.sql.*;

/**
 * @PackageName: com.cn.zpark.utils
 * @ClassName: JDBCUtils
 * @Description:JDBC工具类
 * @author: RZS
 * @date: 2022/3/30  10:57
 */
public class JDBCUtils {
    // 定义数据路连接信息
    private static String url = "jdbc:mysql://192.168.83.128:3306/java_web01?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8";
    private static String user = "root";
    private static String pwd = "root";


    /**
     * 获取数据库连接对象
     * @return
     */
    public static Connection getConn() throws Exception {
        // 注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        // 返回连接对象
        return DriverManager.getConnection(url, user, pwd);
    }

    /**
     * 关闭资源
     * @param conn
     * @param res
     * @param stat
     */
    public static void close(Connection conn, ResultSet res, Statement... stat){
        try {

            if(res != null){
                res.close();
            }

            for (Statement statement : stat) {
                if(statement != null){
                    statement.close();
                }

            }

            assert conn != null;
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
